<UserControl x:Class="Microsoft.Practices.QuickStarts.WPFIntegration.OrganizationChart.OrgChartView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    >
  <UserControl.Resources>

    <XmlDataProvider x:Key="xdpOrgChart" />

    <!-- Data Presentation Template -->
    <HierarchicalDataTemplate
			x:Key="OrgChartDataTemplate"
			DataType="Position"
			ItemsSource ="{Binding XPath=*}">
      <TextBlock Text="{Binding XPath=@Title}" TextWrapping="Wrap"
				FontSize="12"
				FontFamily="Segoe UI"
				MaxWidth="150"
				HorizontalAlignment="Center"
				TextAlignment="Center"
				Style="{DynamicResource OrgPositionStyle}">
      </TextBlock>
    </HierarchicalDataTemplate>

    <!-- Backgrounds definitions. -->
    <LinearGradientBrush x:Key="OrgPositionBackground" StartPoint="1,-2" EndPoint="0,2">
      <GradientStop Color="#DAEEF3" Offset="0.3"/>
      <GradientStop Color="#92CDDC" Offset="0.5"/>
      <GradientStop Color="#DAEEF3" Offset="0.7"/>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="OrgOpenedPositionBackground" StartPoint="1,-2" EndPoint="0,2">
      <GradientStop Color="#FF3300" Offset="0.3"/>
      <GradientStop Color="#9A0000" Offset="0.5"/>
      <GradientStop Color="#FF3300" Offset="0.7"/>
    </LinearGradientBrush>

    <!-- End Backgrounds definitions. -->

    <Style x:Key ="OrgPositionBorderBaseStyle" TargetType="{x:Type Border}">
      <Setter Property="CornerRadius" Value="10"></Setter>
      <Setter Property="Padding" Value="10"></Setter>
      <Setter Property="MinWidth" Value="100"></Setter>
      <Setter Property="VerticalAlignment" Value="Center"></Setter>
      <Setter Property="HorizontalAlignment" Value="Center"></Setter>
      <Setter Property="BorderThickness" Value="1"></Setter>
      <Setter Property="BorderBrush" Value="Black"></Setter>
    </Style>

    <!-- Border Definition for each Position in the Organization Chart. -->
    <Style x:Key="OrgPositionBorderStyle" TargetType="{x:Type Border}" BasedOn="{StaticResource OrgPositionBorderBaseStyle}">
      <Setter Property="Background" Value="{StaticResource OrgPositionBackground}"></Setter>
      <Setter Property="Opacity" Value="0.5" />
      <Setter Property="TextBlock.Foreground" Value="Black" />
      <Style.Triggers>
        <DataTrigger Binding="{Binding XPath=@Status}" Value="Opened">
          <Setter Property="Background" Value="{StaticResource OrgOpenedPositionBackground}"></Setter>
          <Setter Property="TextBlock.Foreground" Value="White" />
        </DataTrigger>
        <Trigger Property="Border.IsMouseOver" Value="True">
          <Trigger.EnterActions>
            <BeginStoryboard>
              <Storyboard TargetProperty="Opacity">
                <DoubleAnimation Duration="0:0:0.5" To="1.0"/>
              </Storyboard>
            </BeginStoryboard>
          </Trigger.EnterActions>
          <Trigger.ExitActions>
            <BeginStoryboard>
              <Storyboard TargetProperty="Opacity">
                <DoubleAnimation Duration="0:0:0.5" To="0.5"/>
              </Storyboard>
            </BeginStoryboard>
          </Trigger.ExitActions>
        </Trigger>
      </Style.Triggers>
    </Style>

    <!-- Organization Chart TreeView Style Definition -->
    <Style x:Key="OrgChartTreeViewStyle" TargetType="{x:Type TreeView}">
      <Setter Property="OverridesDefaultStyle" Value="True" />
      <Setter Property="SnapsToDevicePixels" Value="True" />
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
      <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type TreeView}">
            <Border
						  Name="Border"
						  CornerRadius="1"
						  BorderThickness="1" >
              <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
            </Border>
           </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <Style x:Key="OrgChartTreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
      <Setter Property="Background"
				Value="Transparent"/>
      <Setter Property="IsExpanded" Value="True"></Setter>
      <Setter Property="Padding"
				Value="0,0,0,0"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type TreeViewItem}">
            <Grid>
              <Grid.Margin>5</Grid.Margin>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
              </Grid.RowDefinitions>
              <StackPanel Name="ItemPanel">
                <StackPanel.LayoutTransform>
                  <ScaleTransform />
                </StackPanel.LayoutTransform>
                <StackPanel.Triggers>
                  <EventTrigger RoutedEvent="StackPanel.Loaded">
                    <EventTrigger.Actions>
                      <BeginStoryboard>
                        <Storyboard>
                          <DoubleAnimation DecelerationRatio="1.0" Duration="0:0:1.0" From="0.0" Storyboard.TargetProperty="LayoutTransform.ScaleX"/>
                          <DoubleAnimation DecelerationRatio="1.0" Duration="0:0:1.0" From="0.0" Storyboard.TargetProperty="LayoutTransform.ScaleY"/>
                        </Storyboard>
                      </BeginStoryboard>
                    </EventTrigger.Actions>
                  </EventTrigger>
                </StackPanel.Triggers>
                <Border Style="{StaticResource OrgPositionBorderStyle}"
								Grid.Row="0"
								Grid.Column="0" Name="Bd">
                
                <!-- Content presenter will show a position name. -->
                <ContentPresenter x:Name="PART_Header" ContentSource="Header"/>

              </Border>

              <!-- Reflection effect -->
              <Rectangle Height="{Binding ElementName=Bd, Path=ActualHeight}"
                         Width="{Binding ElementName=Bd, Path=ActualWidth}">
                <Rectangle.Fill>
                  <VisualBrush Visual="{Binding ElementName=Bd}" />
                </Rectangle.Fill>
                <Rectangle.LayoutTransform>
                  <TransformGroup>
                    <ScaleTransform ScaleY="-0.75"/>
                  </TransformGroup>
                </Rectangle.LayoutTransform>
                <Rectangle.OpacityMask>
                  <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Offset="0" Color="Transparent" />
                    <GradientStop Offset="1" Color="#77000000"/>
                  </LinearGradientBrush>
                </Rectangle.OpacityMask>
              </Rectangle>
              </StackPanel>
              <StackPanel Orientation="Horizontal" IsItemsHost="True" x:Name="ItemsHost"
									Grid.Row="1"
									Grid.Column="0"
									Grid.ColumnSpan="2"/>
            </Grid>
            <ControlTemplate.Triggers>
              <Trigger Property="IsExpanded"
								 Value="false">
                <Setter TargetName="ItemsHost"
									Property="Visibility"
									Value="Collapsed"/>
              </Trigger>              
              <Trigger Property="IsSelected" Value="true">
                <Trigger.EnterActions>
                  <BeginStoryboard>
                    <Storyboard>
                      <DoubleAnimation DecelerationRatio="1.0" Duration="0:0:0.5" To="1.2" Storyboard.TargetName="ItemPanel" Storyboard.TargetProperty="LayoutTransform.ScaleX"/>
                      <DoubleAnimation DecelerationRatio="1.0" Duration="0:0:0.5" To="1.2" Storyboard.TargetName="ItemPanel" Storyboard.TargetProperty="LayoutTransform.ScaleY"/>
                    </Storyboard>
                  </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                  <BeginStoryboard>
                    <Storyboard>
                      <DoubleAnimation DecelerationRatio="1.0" Duration="0:0:0.5" To="1.0" Storyboard.TargetName="ItemPanel" Storyboard.TargetProperty="LayoutTransform.ScaleX"/>
                      <DoubleAnimation DecelerationRatio="1.0" Duration="0:0:0.5" To="1.0" Storyboard.TargetName="ItemPanel" Storyboard.TargetProperty="LayoutTransform.ScaleY"/>
                    </Storyboard>
                  </BeginStoryboard>
                </Trigger.ExitActions>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

  </UserControl.Resources>
  <DockPanel>
    <DockPanel.Background>
      <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="{Binding RelativeSource={RelativeSource Self}, Path=Background}" Offset="0.5"/>
        <GradientStop Color="Gray" Offset="1"/>
      </LinearGradientBrush>
    </DockPanel.Background>
    <StackPanel Orientation="Vertical" DockPanel.Dock="Top">
      <TextBlock Margin="5,5,0,0" DockPanel.Dock="Top">
        <Label Content="Zoom: (-)"></Label>
        <Slider Width="300" Name="zoomSlider" Value="1" Maximum="5" Minimum="0.2"></Slider>
        <Label Content="(+)"></Label>
        <LineBreak/>
        <LineBreak/>
        <Label Content="References: "/>
        <Border BorderThickness="1" BorderBrush="Black" Background="{StaticResource OrgPositionBackground}" CornerRadius="3">
          <Label FontFamily="Segoe UI" Content="Closed Positions"></Label>
        </Border>
        <Border BorderThickness="1" BorderBrush="Black" Background="{StaticResource OrgOpenedPositionBackground}" CornerRadius="3">
          <Label FontFamily="Segoe UI" Content="Opened Positions" Foreground="White"></Label>
        </Border>
        <LineBreak/>
      </TextBlock>
    </StackPanel>
    <ScrollViewer Name="scrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" DockPanel.Dock="Bottom">
        <TreeView Name="_treeView" DockPanel.Dock="Top"
          ItemsSource="{Binding Source={StaticResource xdpOrgChart}, XPath=*}"
          ItemTemplate="{DynamicResource OrgChartDataTemplate}"
          ItemContainerStyle="{DynamicResource OrgChartTreeViewItemStyle}"
          Style="{DynamicResource OrgChartTreeViewStyle}" SelectedItemChanged="SelectionChanged">
          <TreeView.LayoutTransform>
            <TransformGroup>
              <ScaleTransform ScaleX="{Binding Value, ElementName=zoomSlider}" ScaleY="{Binding Value, ElementName=zoomSlider}"/>
            </TransformGroup>
          </TreeView.LayoutTransform>
        </TreeView>
    </ScrollViewer>
  </DockPanel>

</UserControl>
